package com.lxl.hadoop.etl;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

/**
 * @Author lixilin
 * @Date 2022/1/8 17:23
 * @Description
 */
public class LogMapper extends Mapper<LongWritable, Text,Text, NullWritable> {

    Text outKey = new Text();
    NullWritable outVal = NullWritable.get();
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        String line = value.toString();
        // 数据解析校验
        boolean result = parseLine(line,context);
        if (result) {
            outKey.set(line);
            context.write(outKey,outVal);
        }
    }

    private boolean parseLine(String line, Context context) {
        String[] fileds = line.split(" ");
        if (fileds.length > 11) {
            context.getCounter("map", "true").increment(1);
            return true;
        }
        context.getCounter("map", "false").increment(1);
        return false;
    }
}
